package com.ghck.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ghck.server.model.PatientEventDay;
import com.ghck.server.model.query.PatientEventDayQuery;
/**
 * 用户健康数据-每种类型，每天只记录一条  <br>
 * t_patient_event_day <br>
 * v1.0 2016-05-22
 * @see com.ajk.server.dao.PatientEventDayMapper
 */
 public class PatientEventDayProvider {
 
 	/**
 	* 新增 用户健康数据-每种类型，每天只记录一条
 	*/
 	public String insert(PatientEventDay domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("insert into t_patient_event_day(create_time,last_modify_time");
	 	if(domain.getPatientEventId()!=null){
			sb.append(",patient_event_id");
		}
	 	if(domain.getDeviceId()!=null){
			sb.append(",device_id");
		}
	 	if(domain.getUserId()!=null){
			sb.append(",user_id");
		}
	 	if(domain.getCategoryId()!=null){
			sb.append(",category_id");
		}
	 	if(domain.getContent()!=null){
			sb.append(",content");
		}
	 	if(domain.getUuid()!=null){
			sb.append(",uuid");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",in_used");
		}
	 	if(domain.getActionTime()!=null){
			sb.append(",action_time");
		}
		
		sb.append(") values ( now(),now() ");
		
	 	if(domain.getPatientEventId()!=null){
			sb.append(",#{patientEventId}");
		}
	 	if(domain.getDeviceId()!=null){
			sb.append(",#{deviceId}");
		}
	 	if(domain.getUserId()!=null){
			sb.append(",#{userId}");
		}
	 	if(domain.getCategoryId()!=null){
			sb.append(",#{categoryId}");
		}
	 	if(domain.getContent()!=null){
			sb.append(",#{content}");
		}
	 	if(domain.getUuid()!=null){
			sb.append(",#{uuid}");
		}
	 	if(domain.getInUsed()!=null){
			sb.append(",#{inUsed}");
		}
	 	if(domain.getActionTime()!=null){
			sb.append(",#{actionTime}");
		}
		sb.append(")");
		
		return sb.toString();
	}

	/**
 	* 更新 用户健康数据-每种类型，每天只记录一条
 	*/
	public String update(PatientEventDay domain){
		StringBuilder sb = new StringBuilder();
		
		sb.append("update t_patient_event_day set last_modify_time = now() ");
		
		if(domain.getPatientEventId()!=null){
			sb.append(",patient_event_id=#{patientEventId}");
		}
		if(domain.getDeviceId()!=null){
			sb.append(",device_id=#{deviceId}");
		}
		if(domain.getUserId()!=null){
			sb.append(",user_id=#{userId}");
		}
		if(domain.getCategoryId()!=null){
			sb.append(",category_id=#{categoryId}");
		}
		if(domain.getContent()!=null){
			sb.append(",content=#{content}");
		}
		if(domain.getUuid()!=null){
			sb.append(",uuid=#{uuid}");
		}
		if(domain.getInUsed()!=null){
			sb.append(",in_used=#{inUsed}");
		}
		if(domain.getActionTime()!=null){
			sb.append(",action_time=#{actionTime}");
		}
		
		sb.append(" where id=#{id}");
		
		return sb.toString();
	}
	
	public String batchInsert(Map<String, List<PatientEventDay>> map){
		StringBuffer sb = new StringBuffer();
		List<PatientEventDay> patientEventDays = map.get("list");
		sb.append("insert into t_patient_event_day(create_time,last_modify_time,patient_event_id,device_id,user_id,category_id,content,uuid,in_used,action_time) values");
	    MessageFormat messageFormat = new MessageFormat("("
	    		+ "now(),now()"
	    		+ ",#'{'list[{0}].patientEventId}"
	    		+ ",#'{'list[{0}].deviceId}"
	    		+ ",#'{'list[{0}].userId}"
	    		+ ",#'{'list[{0}].categoryId}"
	    		+ ",#'{'list[{0}].content}"
	    		+ ",#'{'list[{0}].uuid}"
	    		+ ",#'{'list[{0}].inUsed}"
	    		+ ",#'{'list[{0}].actionTime}"
	    		+")");
	    		
	    if(CollectionUtils.isNotEmpty(patientEventDays)) {
			sb.append(messageFormat.format(new Integer[]{0}));
			for (int i = 1; i <  patientEventDays.size(); i++) {
				sb.append(",").append(messageFormat.format(new Integer[]{i}));
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过query 查询 用户健康数据-每种类型，每天只记录一条
 	*/
	public String findByQuery(PatientEventDayQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select * from t_patient_event_day a ");
		
		//TODO: add your logic
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by create_time desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(PatientEventDayQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(1) from t_patient_event_day a ");
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 用户健康数据-每种类型，每天只记录一条
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from t_patient_event_day where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1=2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	